# --*-- coding:utf-8 --*--

import warnings
warnings.filterwarnings("ignore")
import requests
import datetime
import time
import math


class Category_composition():

	cookies = None
	head = {
			'Host': 'sycm.taobao.com',
			'Connection': 'keep-alive',
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'

	}
	dateRange = None

	def Category(self):

		# 时间处理
		# time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
		# time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
		# dateRange = '%s|%s' %(time_str_N,time_str_p)

		# 获取类目1级类目

		url = 'https://sycm.taobao.com/bda/tradinganaly/constitute/listLevelOneCategory.json'

		data = {
				'dateRange':self.dateRange,
				'dateType':'month',
				'device':'0',
				'token':'3eab7a8b3',
				'_':'1536570946651'
		}

		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		return resp.json()

	def Category_son(self,categoryId):

		#获取子类目及数据

		# 时间处理
		# time_str_p = (datetime.datetime.now() - datetime.timedelta(days = 1)).strftime("%Y-%m-%d") #
		# time_str_N = (datetime.datetime.now() - datetime.timedelta(days = 30)).strftime("%Y-%m-%d") # 30天前
		# dateRange = '%s|%s' %(time_str_N,time_str_p)

		url = 'https://sycm.taobao.com/bda/tradinganaly/constitute/listLfCategory.json'
		data = {
				'categoryId':categoryId,
				'dateRange':self.dateRange,
				'dateType':'month',
				'device':'0',
				'orderDirection':'false',
				'orderField':'payAmt',
				'page':'1',
				'pageLimit':'5',
				'token':'3eab7a8b3',
				'_':'1536570946921'
		}
		sondata = []
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
		SON_DATA = resp.json()
		recordCount = (SON_DATA['data']['recordCount'])
		page_con = math.ceil(recordCount/5)
		for i in range(1,page_con + 1):
			time.sleep(0.5)
			data['page'] = str(i)
			resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
			for k in resp.json()['data']['data']:
				sondata.append(k)
		return sondata


if __name__ == '__main__':
	
	Category_composition = Category_composition()
	Category_composition.dateRange = '2018-05-01|2018-05-31'
	Category_composition.cookies = {
        "dnk": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "tracknick": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "JSESSIONID": "905295D1C65AFF9A4497D093B234BCF1",
        "mt": "np=",
        "t": "9edbf9df142e07eeaedd45f29878831e",
        "_cc_": "U%2BGCWk%2F7og%3D%3D",
        "uc1": "cookie16=UIHiLt3xCS3yM2h4eKHS9lpEOw%3D%3D&tmb=1&cookie21=W5iHLLyFfoWm&cookie15=URm48syIIVrSKA%3D%3D&existShop=true&pas=0&cookie14=UoTYNkUQuU6gIw%3D%3D&tag=8&lng=zh_CN",
        "lgc": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "cna": "cJFTFKDA9n4CAcFwhT5/XTa9",
        "skt": "b91a82b8f5997d4d",
        "unb": "3397628220",
        "cookie1": "UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D",
        "existShop": "MTU0MDI1OTcxNw%3D%3D",
        "cookie17": "UNN0nnQ5PzBj6g%3D%3D",
        "sg": "%E5%BA%9702",
        "cookie2": "1e816f86422a666c13fbc5542a981fa7",
        "uc3": "vt3=F8dByRmn88dYyaK%2BRVQ%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=UtASsssmOIJ0bQ%3D%3D",
        "v": "0",
        "_tb_token_": "ebd7ed58b1e58",
        "_nk_": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "isg": "BD8_7y6jWLjiIFykvmYxmCDOwhrJJJPGmHdhjdEM2-414F9i2fQjFr1yJj4eo2s-",
        "apushd2dce1c5dd89f161d480bff397a6184e": "%7B%22ts%22%3A1540259755117%2C%22parentId%22%3A1540259719993%7D",
        "_l_g_": "Ug%3D%3D",
        "tg": "0",
        "csg": "6cc83e44"
    }

	# 父类
	fat = Category_composition.Category()

	# 处理子类数据
	cateIList = []
	for i in fat['data']['levelOneCategoryBOList']:
		time.sleep(0.5)
		for h in (Category_composition.Category_son(i['cateId'])):
			h['fname'] = i['cateName']
			cateIList.append(h)
	print (cateIList)
